﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Common;

namespace CNCSoft.ErpApps.TMS.longGiaoVu
{
    public partial class frmInvoice_BaoLuu : Form
    {
        private SqlConnection conn = new SqlConnection(GlobalVariable.WSInfo.ConnectionString);
        DataSet ds = new DataSet();
        private string _enquirynum = "";
        private string _invoicenum = "";

        public string Invoicenum
        {
            get { return _invoicenum; }
            set { _invoicenum = value; }
        }

        private double _sotienbaoluu = 0;

        public double Sotienbaoluu
        {
            get { return _sotienbaoluu; }
            set { _sotienbaoluu = value; }
        }

        private bool _baoluu = false;

        public bool Baoluu
        {
            get { return _baoluu; }
            set { _baoluu = value; }
        }

        public frmInvoice_BaoLuu()
        {
            InitializeComponent();
        }

        public frmInvoice_BaoLuu(string enquirynumber)
        {
            InitializeComponent();
            _enquirynum = enquirynumber;
        }

        private void frmInvoice_BaoLuu_Load(object sender, EventArgs e)
        {
            GetBaoLuuByEnquiryNumber(_enquirynum);
        }

        private void lblInvoiceNumber_TextChanged(object sender, EventArgs e)
        {
            
        }

        private void gridView_Invoice_BaoLuu_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            try
            {
                DataRow dr_current = gridView_Invoice_BaoLuu.GetFocusedDataRow();
                _invoicenum = dr_current["InvoiceNumber"].ToString();
                lblInvoiceNumber.Text = _invoicenum;
                Sotienbaoluu = Convert.ToDouble(dr_current["SoTienBaoLuu"]);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private Invoice Inv;
        private void btnApDung_Click(object sender, EventArgs e)
        {
            try
            {
                Baoluu = true;
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            try
            {
                _invoicenum = "";
                Sotienbaoluu = 0;
                Baoluu = false;
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        #region "Coding"
        /*
        private void LoadInvoiceByInvoiceNumber(string invoicenumber)
        {
            try
            {
                if (ds.Tables["TableInvoiceByInvoiceNumber"] != null)
                    ds.Tables["TableInvoiceByInvoiceNumber"].Clear();
                string sql = @"SELECT * FROM Invoice WHERE InvoiceNumber = '" + invoicenumber + "'";
                SqlDataAdapter da = new SqlDataAdapter(sql, conn);
                da.Fill(ds, "TableInvoiceByInvoiceNumber");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        */
        private void GetBaoLuuByEnquiryNumber(string enquirynumber)
        {
            try
            {
                if (ds.Tables["TableBaoLuuByEnquiryNumber"] != null)
                    ds.Tables["TableBaoLuuByEnquiryNumber"].Clear();
                string sql = @"SELECT enq.Holot, enq.Ten, dmkh.TenKhoaHoc, bl.* FROM BaoLuu bl 
	                        INNER JOIN DM_KHOAHOC dmkh ON bl.MaKhoaHoc = dmkh.MaKhoaHoc
	                        INNER JOIN Invoice inv ON inv.InvoiceNumber = bl.InvoiceNumber
	                        INNER JOIN Enquiry enq ON enq.EnquiryNumber = inv.EnquiryNumber
                                WHERE bl.InvoiceNumber IN 
                                (
						            SELECT
						            InvoiceNumber
						            FROM
						            Invoice inv INNER JOIN Enquiry enq ON inv.EnquiryNumber = enq.EnquiryNumber
						            WHERE inv.EnquiryNumber = '" + enquirynumber + @"'
						        )";
                SqlDataAdapter da = new SqlDataAdapter(sql, conn);
                da.Fill(ds, "TableBaoLuuByEnquiryNumber");
                gridControl_Invoice_BaoLuu.DataSource = ds.Tables["TableBaoLuuByEnquiryNumber"];
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        #endregion"
    }
}
